<template>
  <div class="base-filter-tag-list">

    <label class="base-filter-tag-list__label">{{ label }}</label>

    <div ref="collapseRef" class="base-filter-tag-list__collapse-container"
      :class="{ 'base-filter-tag-list__collapse-container--collapse': isCollapse }">
      <div class="base-filter-tag-list__tags">
        <div v-for="item of list" :key="item[props.key || props.value || 'value']" class="base-filter-tag-list__tag"
          :class="{ active: active === item[props.value || 'value'] }" @click="handleClick(item)">
          <slot v-bind="item">
            <span>{{ item[props.label || 'label'] }}</span>
          </slot>
        </div>
      </div>
    </div>

    <div v-if="(collapsable && needCollapse)" v-show="needCollapse" class="base-filter-tag-list__collapse"
      @click="toggleCollapse">
      <span>{{ isCollapse ? $t('common.base.expand') : $t('common.base.collapse') }}</span>
      <i :class="['iconfont', isCollapse ? 'icon-down' : 'icon-Icon_up']" />
    </div>
  </div>
</template>
<script>
import { defineComponent, toRef } from 'vue';
import { useCollapse } from './useCollapse'

export default defineComponent({
  name: 'BaseFilterTagList',
  props: {
    label: {
      typr: String,
      default: ''
    },
    list: {
      type: Array, // as PropType<Record<string, any>[]>,
      default: () => [],
    },
    active: {
      type: [String, Number],
      default: '',
    },
    collapsable: {
      type: Boolean,
      default: true,
    },
    props: {
      tyype: Object, // as PropType<{ label: string, value: string, key: string }>,
      default: () => ({
        label: 'label',
        value: 'value',
        key: 'value'
      })
    }
  },
  setup(props, { emit }) {

    const { collapseRef, isCollapse, needCollapse, calcCollapse, toggleCollapse } = useCollapse(toRef(props, 'collapsable'), emit)
    
    function handleClick(item) {
      emit('choose', item);
    }

    return {
      collapseRef,
      isCollapse,
      needCollapse,
      toggleCollapse,
      handleClick,
    };
  },
});
</script>
<style lang="scss">
.base-filter-tag-list {
  flex: 1;
  width: 100%;
  display: flex;
  color: #A7B5B5;

  .base-filter-tag-list__label {
    width: 100px;
    color: #8C8C8C;
    overflow: hidden;
    padding-top: 4px;
    padding-right: 4px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .base-filter-tag-list__collapse-container {
    flex: 1;

    &--collapse {
      height: 30px;
      overflow: hidden;
    }

    .base-filter-tag-list__tags {
      flex: 1;
      display: flex;
      flex-wrap: wrap;

      .base-filter-tag-list__tag {
        max-width: 160px;
        padding: 4px 8px;
        margin: 0 12px 8px 0;
        color: #595959;
        font-size: 13px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        border-radius: 4px;
        border: 1px solid transparent;
        cursor: pointer;

        &.active {
          padding: 4px 8px;
          color: $color-primary;
          background: $color-primary-light-1;
          border-color: $color-primary-light-2;
        }
      }
    }
  }

  .base-filter-tag-list__collapse {
    padding: 4px 8px;
    font-size: 12px;
    color: #8C8C8C;
    cursor: pointer;

    i {
      font-size: 12px;
    }
  }
}
</style>
